package com.mapbased.wfw.handler;

import javax.servlet.http.*;

import org.dom4j.*;
import com.mapbased.wfw.*;
import com.mapbased.cc.CacheHelper;
import com.mapbased.cc.CacheHelper.LastModifyGetter;

/**
 * <p>Title: </p>
 *
 * <p>Description:mainly for home page,that without a queryurl </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: mapbased.com</p>
 *
 * @author chy_hs(Baily)
 * @version 1.0
 */
public class CacheHandler
    extends PageHandler implements HttpConstants,LastModifyGetter

{
     protected long lastModified=System.currentTimeMillis();

    private CacheHelper cache=new CacheHelper();
    public static final String UPDATE_INTERVAL="update-interval";
    public static final String REDIR_LOCALE="redirect-locale";

    public Object dispatchAction(RenderContext ctx)
    {
        HttpServletRequest req=ctx.getRequest();

        // if(!req.getMethod().equals("GET")||req.getQueryString()!=null)
        String md=req.getMethod();
        if(!md.equals("GET")&&!md.equals("HEAD"))
        {
            return super.dispatchAction(ctx);
        }
        long l=cache.filterRequest(ctx);

        Object o=super.dispatchAction(ctx);
        if(o == null)
        {
            cache.addTimeHeader(ctx,l);

        }
        return o;

    }

    protected void fetchAttributs(Element ele)
    {
        cache.setLastModifyGetter(this);

        Attribute a=ele.attribute(UPDATE_INTERVAL);
        if(a != null)
        {

            try
            {
                cache.setUpdateInterval(Integer.parseInt(a.getValue()));
            }
            catch(NumberFormatException ex)
            {
                ex.printStackTrace();
            }
            ele.remove(a);
        }
        a=ele.attribute(REDIR_LOCALE);
        if(a != null)
        {
            try
            {
                cache.setRedirectLocale(Boolean.valueOf(a.getValue()).
                                        booleanValue());
            }
            catch(NumberFormatException ex)
            {
                ex.printStackTrace();
            }
            ele.remove(a);

        }

    }

    /**
     * getLastModified
     *
     * @param rc RenderContext
     * @return long
     * @todo Implement this com.mapbased.cc.CacheHelper.LastModifyGetter
     *   method
     */
    public long getLastModified(RenderContext rc)
    {
        long l=this.cache.getUpdateInterval();
        if(l != 0)
        {
            long ct=System.currentTimeMillis();
            if(ct - this.lastModified > l * 1000)
            {
                this.lastModified=ct;
            }
        }
        return this.lastModified;
    }

    /**
     * init
     *
     * @param global GlobalRef
     * @todo Implement this com.mapbased.wfw.PageHandler method
     */
    protected void init(GlobalRef g)
    {
        super.init(g);
        cache.setUpdateInterval(this.getPageConfigInt(g,UPDATE_INTERVAL,
            cache.getUpdateInterval()));
        cache.setRedirectLocale(
            "true".equalsIgnoreCase(
                this.getPageConfig(g,REDIR_LOCALE,cache.isRedirectLocale() + "").trim()));

    }

}
